﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;

namespace Tesla.Web
{
    public class TimeSheetServiceDispatcher
    {
        public IList<BaseDC> ReadTasksForTimeSheet(int ProjectID, int FinYr, int EmpID)
        {
            ISvcTimeSheet _proxySvcTS;
            ChannelFactory<ISvcTimeSheet> chfTS = null;
            IList<BaseDC> timeSheet = null;
            try
            {
                chfTS = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTimeSheet>();
                _proxySvcTS = chfTS.CreateChannel();
                timeSheet =_proxySvcTS.ReadTasksForTimeSheet(ProjectID, FinYr, EmpID);
            }
            catch (FaultException gfx)
            {
                if (chfTS.State == CommunicationState.Faulted || chfTS.State == CommunicationState.Opened)
                    chfTS.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfTS.State == CommunicationState.Faulted || chfTS.State == CommunicationState.Opened)
                    chfTS.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTS.Close();
            }
            return timeSheet;
        }

        public IList<BaseDC> ReadWeeksCalendar(int ProjectID, int FinYr, DateTime refdate)
        {
            ISvcTimeSheet _proxySvcTS;
            ChannelFactory<ISvcTimeSheet> chfTS = null;
            IList<BaseDC> days = null;
            try
            {
                chfTS = GADNChannelFactoryProvider.CreateChannelFactory<ISvcTimeSheet>();
                _proxySvcTS = chfTS.CreateChannel();
                days = _proxySvcTS.ReadWeeksCalendar(ProjectID, FinYr, refdate);
            }
            catch (FaultException gfx)
            {
                if (chfTS.State == CommunicationState.Faulted || chfTS.State == CommunicationState.Opened)
                    chfTS.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfTS.State == CommunicationState.Faulted || chfTS.State == CommunicationState.Opened)
                    chfTS.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfTS.Close();
            }
            return days;
        }
    }
}